/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rpt.bd.cenario;

import java.awt.Component;
import java.util.Vector;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTabbedPane;
import javax.swing.ListCellRenderer;
import rpt.lib.CCalendarioComboBox;
import rpt.lib.CCoresComboBox;

/**
 *
 * @author Roberto
 */
/**************************************************************************/
/***                                                                    ***/
/**************************************************************************/

public class CEstruturaCampoRegistroBdLookup extends CEstruturaCampoRegistroBd {

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    private CCenarioBd fCenarioBdLookup = null;
    private String fNomeTabelaLookup = null;
    private String fAliasTabelaLookup = null;
    private String fNomeChavePrimariaLookup = null;
    private String fNomeCampoDescricaoLookup = null;
    private Object fCondicaoLookup = null;

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CEstruturaCampoRegistroBdLookup() {
        super();
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CEstruturaCampoRegistroBdLookup(
        final CEstruturaRegistroBd aEstruturaRegistroBd,
        final String aTitulo,
        final String aNomeBd,
        final Component aComponenteCampo,
        final String aValorOriginal,
        final ETipoCampoBd aTipoCampo,
        final boolean aCampoGradeBd,
        final boolean aCampoObrigatorio,
        final int aLarguraGrade,
        final boolean aEditavel,
        final boolean aSelect,
        final boolean aInsert,
        final boolean aUpdate,
        final String aNomeTabelaLookup,
        final String aAliasTabelaLookup,
        final String aNomeChavePrimariaLookup,
        final String aNomeCampoDescricaoLookup,
        final Object aCondicaoLookup,
        final int aPosicaoColunaRelatorio,
        final int aLarguraColunaRelatorio) {
        //
        super(aEstruturaRegistroBd,aTitulo,aNomeBd,aComponenteCampo,aValorOriginal,
            aTipoCampo,aCampoGradeBd,aCampoObrigatorio,aLarguraGrade,
            aEditavel,aSelect,aInsert,aUpdate,aPosicaoColunaRelatorio,aLarguraColunaRelatorio);
        //
        Vector vDados = null;
        //
        fNomeChavePrimariaLookup = aNomeChavePrimariaLookup;
        fNomeCampoDescricaoLookup = aNomeCampoDescricaoLookup;
        fCondicaoLookup = aCondicaoLookup;
        //
        if (false) { // (aNomeTabelaLookup instanceof CConjuntoDadosBd) {
            // final CConjuntoDadosBd c = (CConjuntoDadosBd)aNomeTabelaLookup;
            // vDados = c.getConjuntoDados();
        }
        else {
            fNomeTabelaLookup = (String)aNomeTabelaLookup;
            if (aAliasTabelaLookup == null)
                fAliasTabelaLookup = (String)aAliasTabelaLookup;
            else
                fAliasTabelaLookup = (String)fNomeTabelaLookup;
            if (fAliasTabelaLookup == null)
                fAliasTabelaLookup = fNomeTabelaLookup;
            //
            CDialogoLookup vDialogoLookup = new CDialogoLookup(
                fGlobal.getConectorBd(),
                null,
                fNomeTabelaLookup,
                fAliasTabelaLookup,
                fNomeChavePrimariaLookup,
                fNomeCampoDescricaoLookup,
                fCondicaoLookup);
            //
            JFrame vJanelaLookup = new JFrame();
            JTabbedPane vPainelLookup = new JTabbedPane();
            vJanelaLookup.setContentPane(vPainelLookup);
            //
            fCenarioBdLookup =
                new CCenarioBdEdicao(
                    aTitulo,
                    fGlobal,
                    1,
                    vDialogoLookup,
                    vPainelLookup
                );
            //
            prepararComboBox(fCenarioBdLookup.getPertinenciaBd().getConjuntoDadosBd());
            //
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    class ComboBoxRenderer extends JLabel
                           implements ListCellRenderer {

        public ComboBoxRenderer() {
            setOpaque(true);
            setHorizontalAlignment(LEFT);
            setVerticalAlignment(CENTER);
        }

        /*
         * This method finds the image and text corresponding
         * to the selected value and returns the label, set up
         * to display the text and image.
         */
        public Component getListCellRendererComponent(
                                           JList list,
                                           Object value,
                                           int index,
                                           boolean isSelected,
                                           boolean cellHasFocus) {
            //Get the selected index. (The index param isn't
            //always valid, so just use the value.)
            CRegistroBd r = (CRegistroBd)value;
            setText(r.getCampoRegistroBd(1).getValorCampoBdStr());
            return this;
    /*
            int selectedIndex = ((Integer)value).intValue();

            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }
            //Set the icon and text.  If icon was null, say so.
            ImageIcon icon = images[selectedIndex];
            String pet = petStrings[selectedIndex];
            setIcon(icon);
            if (icon != null) {
                setText(pet);
                setFont(list.getFont());
            } else {
                setUhOhText(pet + " (no image available)",
                            list.getFont());
            }
    */
        }
    }
    
    public void prepararComboBox(final CConjuntoDadosBd cd) {
        //CConjuntoDadosBd cd = fCenarioBdLookup.getPertinenciaBd().getConjuntoDadosBd();
        Vector vDados = cd.getConjuntoDados();
        CRegistroBd r = new CRegistroBd(fCenarioBdLookup.getPertinenciaBd(),null);
//          CCampoRegistroBd cr = r.getCampoRegistroBdChavePrimaria();
        r.setValorCampoRegistroBdChavePrimaria(null);
        r.setValorCampoRegistroBdDescricao("<SELECIONAR>");
        vDados.insertElementAt(r,0);
        //
        if ((fComponenteCampo instanceof JComboBox) &&
                (!(fComponenteCampo instanceof CCalendarioComboBox)) && 
                (!(fComponenteCampo instanceof CCoresComboBox))){
            JComboBox c = (JComboBox)fComponenteCampo;
            DefaultComboBoxModel m = new DefaultComboBoxModel(vDados);
            c.setModel(m);
            c.setRenderer(new ComboBoxRenderer());
            m.setSelectedItem(r);
            //CRegistroBd r = (CRegistroBd)v.get();
            //c.add();
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CCampoRegistroBd getCampoBdChavePrimaria() {
        CCampoRegistroBd vValor = null;
        if (fComponenteCampo instanceof JComboBox) {
            JComboBox vComboBox = (JComboBox)fComponenteCampo;
            ComboBoxModel m = vComboBox.getModel();
            CRegistroBd rl = (CRegistroBd)m.getSelectedItem();
            vValor = rl.getCampoRegistroBd(fNomeChavePrimariaLookup);
        }
        return vValor;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public String getNomeTabelaLookup() {
        return fNomeTabelaLookup;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public String getAliasTabelaLookup() {
        return fAliasTabelaLookup;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public String getNomeChavePrimariaLookup() {
        return fNomeChavePrimariaLookup;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public String getNomeCampoDescricaoLookup() {
        return fNomeCampoDescricaoLookup;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public String getAliasNomeCampoDescricaoLookup() {
        return fAliasTabelaLookup+"_"+fNomeCampoDescricaoLookup;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public Object getCondicaoLookup() {
        return fCondicaoLookup;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void recarregarDados() {
        if (fCenarioBdLookup != null) {
            CConjuntoDadosBd d = fCenarioBdLookup.getConjuntoDadosBd();
            if (d != null) {
                final String q = getQuery();
                if (q != null) {
                     d.recarregarConjuntoDados(q);
                } else {
                    d.recarregarConjuntoDados();
                }
                prepararComboBox(d);
            }
        }
    }

    /**
     * 
     */

    public String getQuery() {
        return null;
    }
    
    /**
     * 
     * @param aRegistro 
     */
    
    @Override
    public void eventoDepoisMoverRegistro(CRegistro aRegistro) {
        recarregarDados();
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
/*
    public String getValorChavePrimariaStr() {
        String vValor = null;
        if (fComponenteCampo instanceof JComboBox) {
            JComboBox vComboBox = (JComboBox)fComponenteCampo;
            ComboBoxModel m = vComboBox.getModel();
            CRegistroBd rl = (CRegistroBd)m.getSelectedItem();
            return rl.getCampoRegistroBd(fNomeChavePrimariaLookup).toString();
        }
        return vValor;
    }
*/
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
/*
    public String getValorComboBoxStr(JComboBox aComboBox) {
        ComboBoxModel m = aComboBox.getModel();
        CRegistroBd vLocalizado = null;
        int vNumItems = m.getSize();
        for (int vContador = 0; ((vContador < vNumItems) && (vLocalizado == null)); vContador++) {
            CRegistroBd rl = (CRegistroBd)m.getElementAt(vContador);
            if (rl.getCampoRegistroBd(fNomeChavePrimariaLookup).toString().equals(aValor.toString())) {
                m.setSelectedItem(rl);
                vLocalizado = rl;
            }
        }
    }
*/
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    @Override
    public int setValorComboBox(Object aValor) {
        JComboBox cb = (JComboBox)fComponenteCampo;
        ComboBoxModel m = cb.getModel();
        if (aValor == null) {
            CRegistroBd rl = (CRegistroBd)m.getElementAt(0);
            m.setSelectedItem(rl);
        } else {
            CRegistroBd vLocalizado = null;
            int vNumItems = m.getSize();
            for (int vContador = 0; ((vContador < vNumItems) && (vLocalizado == null)); vContador++) {
                CRegistroBd rl = (CRegistroBd)m.getElementAt(vContador);
                CCampoRegistroBd c = rl.getCampoRegistroBd(fNomeChavePrimariaLookup);
                if ((c != null) && (c.getValorCampoBd() != null)) {
                    if (c.toString().equals(aValor.toString())) {
                        m.setSelectedItem(rl);
                        vLocalizado = rl;
                    }
                }
            }
        }
        return 1;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    
}
